package com.cskaoyan.service.wx_comment;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cskaoyan.bean.bo.wx_comment.WxCommentListBO;
import com.cskaoyan.bean.bo.wx_comment.WxCommentPostBO;
import com.cskaoyan.bean.po.wx_comment.WxCommentPostOutPO;
import com.cskaoyan.bean.po.wx_comment.WxCommentPostPO;
import com.cskaoyan.bean.po.wx_comment.WxCommentListPO;
import com.cskaoyan.bean.po.wx_comment.WxCommentUserInfoPO;
import com.cskaoyan.bean.vo.wx_comment.*;
import com.cskaoyan.mapper.wx_comment.WxCommentListMapper;
import com.cskaoyan.mapper.wx_comment.WxCommentPostMapper;
import com.cskaoyan.mapper.wx_comment.WxCommentPostOutMapper;
import com.cskaoyan.mapper.wx_comment.WxCommentUserInfoMapper;
import com.cskaoyan.util.beanutils.BeanUtil;
import com.cskaoyan.util.common.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: 冯子靖
 * @Description:
 * @Date: 2022/7/21 21:32
 */
@Service
public class WxCommentServiceImpl implements WxCommentService {


    @Autowired
    WxCommentListMapper wxCommentListMapper;

    @Autowired
    WxCommentUserInfoMapper wxCommentUserInfoMapper;

    @Autowired
    WxCommentPostMapper wxCommentPostMapper;

    @Autowired
    WxCommentPostOutMapper wxCommentPostOutMapper;

    /**
     * 获取评论list
     *
     * @param wxCommentListBO
     * @return
     */
    @Override
    public WxCommentListVO printCommentList(Integer currentUserId, WxCommentListBO wxCommentListBO) {

        Integer page = wxCommentListBO.getPage();
        Integer limit = wxCommentListBO.getLimit();
        //获取评论类型 0商品 or 1专题
        Integer commentType = wxCommentListBO.getType();
        //获取valueId,有待判断
        Integer valueId = wxCommentListBO.getValueId();
        //获取showType,0无图,1有图
        Integer showType = wxCommentListBO.getShowType();

        //开启分页
        PageHelper.startPage(page, limit);
        /**
         * TODO
         */
        QueryWrapper<WxCommentListPO> CommentListWrapper = new QueryWrapper<>();


        ArrayList<WxCommentsListVO> wxCommentsListVOS = new ArrayList<>();
        if (showType == 1) {
            //商品评论,valueId为商品id


            CommentListWrapper.eq("value_id", valueId)
                    .eq("type", commentType)
                    .eq("deleted", 0)
                    .eq("has_picture", showType)
                    .orderByDesc("add_time");

            //符合要求的所有评论

        } else {

            //专题评论,valueId为专题id
            CommentListWrapper.eq("value_id", valueId)
                    .eq("type", commentType)
                    .eq("deleted", 0)
                    .orderByDesc("add_time");
            //符合要求的所有评论

        }
        List<WxCommentListPO> wxCommentListPOS = wxCommentListMapper.selectList(CommentListWrapper);
        for (WxCommentListPO comment : wxCommentListPOS) {

            Integer userId = comment.getUserId();
            WxCommentsListVO wxCommentsListVO = new WxCommentsListVO();
            WxCommentsUserInfoVO wxCommentsUserInfoVO = new WxCommentsUserInfoVO();

            WxCommentUserInfoPO wxCommentUserInfoPO = wxCommentUserInfoMapper.selectById(userId);


            WxCommentUserInfoPO userInfo = new WxCommentUserInfoPO(wxCommentUserInfoPO.getNickname(), wxCommentUserInfoPO.getAvatar());
            BeanUtil.copyBean(userInfo, wxCommentsUserInfoVO);
            //评论存入vo
            try {
                BeanUtils.copyProperties(wxCommentsListVO, comment);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            //图片存入vo
            String[] strings = StringUtils.splitJsonArrays(comment.getPicUrls());
            wxCommentsListVO.setPicList(strings);
            //userInfo存入vo
            wxCommentsListVO.setUserInfo(wxCommentsUserInfoVO);

            wxCommentsListVOS.add(wxCommentsListVO);
        }
        PageInfo<WxCommentListPO> wxCommentListPOPageInfo = new PageInfo<>(wxCommentListPOS);
        int total = (int) wxCommentListPOPageInfo.getTotal();
        int pages = wxCommentListPOPageInfo.getPages();
        return new WxCommentListVO(limit, page, pages, total, wxCommentsListVOS);
    }

    @Override
    public WxCountCommentVO countComment(Integer valueId, Integer type) {

        QueryWrapper<WxCommentListPO> wxCommentListPOQueryWrapper = new QueryWrapper<>();

        QueryWrapper<WxCommentListPO> wxCommentListPOQueryWrapper1 = new QueryWrapper<>();
        wxCommentListPOQueryWrapper.eq("has_picture", 1)
                .eq("value_id", valueId)
                .eq("type", type);

        wxCommentListPOQueryWrapper1.eq("type", type)
                .eq("value_id", valueId);

        //统计type0/1的当前商品或专题的所有数据
        Integer allCount = wxCommentListMapper.selectCount(wxCommentListPOQueryWrapper1);
        //统计
        Integer hasPicCount = wxCommentListMapper.selectCount(wxCommentListPOQueryWrapper);

        return new WxCountCommentVO(allCount, hasPicCount);

    }

    @Override
    public WXCommentPostVO postComment(Integer userId, WxCommentPostBO wxCommentPostBO) {

        if (userId == -1) {
            return new WXCommentPostVO();
        }

        Date date = new Date();
        WxCommentPostPO wxCommentPostPO = new WxCommentPostPO();
        try {
            BeanUtils.copyProperties(wxCommentPostPO, wxCommentPostBO);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        wxCommentPostPO.setAddTime(date);
        wxCommentPostPO.setUpdateTime(date);
        wxCommentPostPO.setUserId(userId);
        wxCommentPostPO.setValueId(Integer.valueOf(wxCommentPostBO.getValueId()));


        //数据库新增评论
        wxCommentPostMapper.insert(wxCommentPostPO);

        WxCommentPostOutPO wxCommentPostOutPO = wxCommentPostOutMapper.selectById(wxCommentPostPO.getId());
        WXCommentPostVO wxCommentPostVO = new WXCommentPostVO();
        try {
            BeanUtils.copyProperties(wxCommentPostVO, wxCommentPostOutPO);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        String[] strings = StringUtils.splitJsonArrays(wxCommentPostOutPO.getPicUrls());
        wxCommentPostVO.setPicUrls(strings);

        return wxCommentPostVO;
    }


}
